package cn.roisrosy.middleware.db.router.config;

import cn.roisrosy.middleware.db.router.DBRouterConfig;
import cn.roisrosy.middleware.db.router.DBRouterJoinPoint;
import cn.roisrosy.middleware.db.router.dynamic.DynamicDataSource;
import cn.roisrosy.middleware.db.router.util.PropertyUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/***
 * 数据源配置解析
 */
@Configuration
public class DataSourceAutoConfig implements EnvironmentAware {
    /**
     * 分库数量
     */
    private int dbCount;

    /**
     * 分表数量
     */
    private int tbCount;

    /**
     * 路由字段
     */
    private String routerKey;

    /**
     * 数据源配置组
     */
    private Map<String, Map<String, Object>> dataSourceMap = new HashMap<>();

    @Bean(name = "db-router-point")
    @ConditionalOnMissingBean
    public DBRouterJoinPoint point(DBRouterConfig dbRouterConfig) {
        return new DBRouterJoinPoint(dbRouterConfig);
    }
    @Bean
    public DBRouterConfig dbRouterConfig() {
        return new DBRouterConfig(dbCount, tbCount, routerKey);
    }
    @Bean
    public DataSource dataSource(){
        Map<Object,Object> targetDataSources = new HashMap<>();
        for(String dbInfo: dataSourceMap.keySet()){
            Map<String,Object> objMap = dataSourceMap.get(dbInfo);
            targetDataSources.put(dbInfo,new DriverManagerDataSource(objMap.get("url").toString(),objMap.get("username").toString(),objMap.get("password").toString()));
        }
        //设置数据源
        DynamicDataSource dataSource =new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSources);
        return  dataSource;
    }

    /***
     * 数据源配置提取
     * @param environment
     */
    @Override
    public void setEnvironment(Environment environment) {
        String prefix ="router.jdbc.datasource.";
        dbCount =Integer.valueOf(environment.getProperty(prefix+"dbCount"));
        tbCount =Integer.valueOf(environment.getProperty(prefix+"tbCount"));
        routerKey=environment.getProperty(prefix+"routerKey");
        String dataSources = environment.getProperty(prefix+"list");
        for(String dbInfo:dataSources.split(",")){
            final String dbPrefix = prefix + dbInfo;
            Map<String,Object> dataSourceProps=PropertyUtil.handle(environment,dbPrefix, Map.class);
            dataSourceMap.put(dbInfo, dataSourceProps);
        }
        //默认数据源
        String defaultData = environment.getProperty(prefix + "default");
        for(String dbInfo:defaultData.split(",")){
            final String dbPrefix = prefix + dbInfo;
            Map<String,Object> dataSourceProps=PropertyUtil.handle(environment,dbPrefix, Map.class);
            dataSourceMap.put(dbInfo, dataSourceProps);
        }

    }
}
